package com.zm.demo.core.leetcode;

import static java.lang.System.out;

/**
 * @author zoum
 * @create 2020/1/9 15:16
 */
public class UniquePathsWithObstacles {

    public int uniquePaths(int[][]obstacleGrid) {
        if (obstacleGrid == null || obstacleGrid.length == 0 || obstacleGrid[0] == null || obstacleGrid[0].length == 0){
            return 0;
        }

        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;
        int[][]initT = new int[m][n];

        for (int i = 0; i < m; i++){
            for (int j = 0; j < n; j++){
                initT[i][j] = obstacleGrid[i][j] == 1 ? 0 : 1;
            }
        }

        boolean flag = false;
        for (int i = 0; i < m; i++){
            if (initT[i][0] == 0){
                flag = true;
                continue;
            }
            if (flag){
                initT[i][0] = 0;
            }
        }

        boolean flagJ = false;
        for (int j = 0; j < n; j++){
            if (initT[0][j] == 0){
                flagJ = true;
                continue;
            }
            if (flagJ){
                initT[0][j] = 0;
            }
        }

        for (int i = 1; i < m; i++){
            for (int j = 1; j < n; j++){
                if (initT[i][j] != 0){
                    initT[i][j] = initT[i-1][j] + initT[i][j-1];
                }
            }
        }

        return initT[m-1][n-1];
    }




//    public static void main(String[]args){
//        out.println(new UniquePathsWithObstacles().uniquePaths(7,3));
//    }
}
